<?php
/**
 * Geneone: Content Management Framework
 * Object Drafts Class
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */

class Gene_Object_Draft {
	/**
	 * Saves a draft of an object. 
	 *
	 * @static
	 *
	 * @var string $type Object Type
	 * @var string $title Object Title
	 * @var string $desc Object Description
	 * @var string $content Object Contents
	 * @var object $parentobj Instance of parent object
	 * @var array $options New Object options
	 * @var integer $update Draft ID to Update
	 *
	 * @return mixed Returns the draft ID (integer) on success or PEAR_Error on failure
	 */
	function save($type, $title='', $desc='', $content='', &$parentobj, $options=array(), $update=false) {
		$db =& Gene::getDatabase();
		$user =& Gene::getUser();
		
		if (!$user->getUserId()) {
			return PEAR::raiseError("You must be signed in to save a draft.");
		}
		
		$parent = $parentobj->get();
		
		$info = array(
			"user_id" => $user->getUserId(),
			"parent_id" => $parent['object_id'],
			"type" => $type,
			"title" => $title,
			"description" => $desc,
			"content" => Gene_Object::cleanHtml($content),
			"draft_time" => time(),
			"extra" => serialize($options)
		);
		
		if ($update) {
			$db->extended->autoExecute(Gene::tableName("object_drafts"), $info, MDB2_AUTOQUERY_UPDATE, "draft_id=".$update);
			return $update;
		} else {
			if ($user->getProperty(Gene::tableName('object_drafts')) >= Gene::getSetting("maximum_drafts")) {
				return PEAR::raiseError("You have exceeded the maximum amount of allowed drafts. This draft has not been saved. Please remove some old drafts.");
			} 
		
			$draftid = $db->nextId(Gene::tableName("object_drafts"));
			$info['draft_id'] = $draftid;
			$db->extended->autoExecute(Gene::tableName("object_drafts"), $info, MDB2_AUTOQUERY_INSERT);
			
			$user->setProperty(Gene::tableName('object_drafts'), $user->getProperty(Gene::tableName('object_drafts'))+1);
		
			return $draftid;
		}
	}
	
	/**
	 * Loads the draft of an object.
	 *
	 * @static
	 *
	 * @var integer $draftid Draft ID
	 * @var object $user User
	 *
	 * @return mixed Draft Information if successful, otherwise false
	 */
	function load($draftid, &$user) {
		$db =& Gene::getDatabase();
	
		$q = $db->prepare ("SELECT * FROM ".Gene::tableName("object_drafts")." WHERE draft_id=?");
		$res = $q->execute(array((int)$draftid));
	
		$draft = $res->fetchRow();
		
		if ($draft['user_id'] == $user->getUserId()) {
			$draft['extra'] = unserialize($draft['extra']);
			return $draft;
		}
		
		return false;
	}
	
	/**
	 * Lists drafts by this user.
	 *
	 * @static
	 *
	 * @var object $user User
	 * @var string $order Order By Clause
	 *
	 * @return array List of Drafts by user
	 */
	function listDrafts(&$user, $order="draft_time DESC") {
		$db =& Gene::getDatabase();
		
		$q = $db->prepare ("SELECT d.*, o.title as parenttitle, o.alias as parentalias FROM ".Gene::tableName("object_drafts")." d LEFT JOIN ".Gene::tableName("objects")." o ON (d.parent_id=o.object_id) WHERE d.user_id=? ORDER BY ".$order);
		$res = $q->execute(array($user->getUserId()));
		
		return $res->fetchAll();
	}
	
	/**
	 * Removes one of the current user's drafts.
	 *
	 * @static
	 *
	 * @var integer $draftid Draft ID
	 *
	 * @return boolean Success
	 */
	function remove($draftid) {
		$db =& Gene::getDatabase();
		$user =& Gene::getUser();
		
		if ($draft = Gene_Object_Draft::load($draftid, $user)) {
			if ($draft['user_id'] == $user->getUserId()) {
				$q = $db->prepare ("DELETE FROM ".Gene::tableName("object_drafts")." WHERE draft_id=?");
				$q->execute(array((int)$draftid));
				
				$user->setProperty(Gene::tableName('object_drafts'), $user->getProperty(Gene::tableName('object_drafts'))-1);
				
				return true;
			}
			
			return false;
		}
		
		return false;
	}
}

?>